{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3f63c62b-b958-462d-abe2-37ac1ea6dbc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "475153cbbf364abc9dd6d83c63f842bc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=320, width=640)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=640,height=320)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "theturtle=turtle.Turtle(myTS)\n",
    "rabbit=turtle.Turtle(myTS,isHolonomic=True)\n",
    "rabbit.shape(\"triangle\")\n",
    "theturtle.shape(\"turtle\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "46fef468-21b8-4909-99f9-571a82827ee3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "胜出者是海龟\n"
     ]
    }
   ],
   "source": [
    "theturtle.penup()\n",
    "rabbit.penup()\n",
    "theturtle.setpos(-320,100)\n",
    "rabbit.setpos(-320,-100)\n",
    "import random\n",
    "t=-320\n",
    "r=-320\n",
    "while t <320 and r <320:\n",
    "    random_i=random.randint(1,100)\n",
    "    if random_i<85:\n",
    "        t += 1\n",
    "    else:\n",
    "        r += 5\n",
    "    theturtle.setpos(t,100)\n",
    "    rabbit.setpos(r,-100)\n",
    "\n",
    "if t<r:\n",
    "    print(\"胜出者是兔子\")\n",
    "else:\n",
    "    print(\"胜出者是海龟\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "13bef0c1-da19-47c4-bc76-4910b4c14425",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b91c7a76e26b423c83b1e910a29683dd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=320, width=640)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=640,height=320)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "theturtle=turtle.Turtle(myTS)\n",
    "rabbit=turtle.Turtle(myTS,isHolonomic=True)\n",
    "rabbit.shape(\"triangle\")\n",
    "theturtle.shape(\"turtle\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab960d8a-2312-4ffa-82b5-dd700c931cad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入比赛次数  1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "海龟赢了1次，兔子赢了0次\n"
     ]
    }
   ],
   "source": [
    "t_win =0\n",
    "r_win =0\n",
    "#分别存储两个的胜利次数\n",
    "num =input(\"输入比赛次数 \")\n",
    "num =eval(num)\n",
    "for x in range(num):\n",
    "    #运行n次这个程序\n",
    "\n",
    "    theturtle.penup()\n",
    "    rabbit.penup()\n",
    "    theturtle.setpos(-320,100)\n",
    "    rabbit.setpos(-320,-100)\n",
    "    import random\n",
    "    t=-320\n",
    "    r=-320\n",
    "    #设置初始位置\n",
    "    \n",
    "    \n",
    "    while t <320 and r <320:\n",
    "        random_i=random.randint(1,100)\n",
    "        if random_i<85:\n",
    "            t += 1\n",
    "        else:\n",
    "            r += 5\n",
    "        theturtle.setpos(t,100)\n",
    "        rabbit.setpos(r,-100)\n",
    "        #完成一次，得到一个结果\n",
    "\n",
    "    if t>r:\n",
    "        t_win +=1\n",
    "    else:\n",
    "        r_win +=1\n",
    "\n",
    "print(f\"海龟赢了{t_win}次，兔子赢了{r_win}次\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a0d75ed-3b66-48e8-887f-9a04007df784",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
